<?php
/*
Plugin Name: Synergy Master
Plugin Author: Maia Engeli
Description: This plugin provides the top area with announcement and menu for the Synergy project
*/

// import javascript file into syn_proj_mgmt plugin
wp_enqueue_script('syn_master_script', '/wp-content/plugins/syn_master/syn_master.js');


function syn_set_current_project($pid) {
	global $wpdb;
	$table = $wpdb->prefix."master";
	$user = wp_get_current_user();
		
	$query = "INSERT INTO $table (`uid` , `pid`) 
	          VALUES ($user->ID , $pid)
              ON DUPLICATE KEY UPDATE `pid` = $pid ;";
	$wpdb->query($query);		  
			  
}

function syn_no_current_project() {
	syn_set_current_project(-1);
}

/**
 * This function returns an integer
 * the current project ID of the current user
 * OR -1 if there is no current project 
 */
function syn_get_current_project() {
	global $wpdb;
	$table = $wpdb->prefix."master";
	$projects = $wpdb->prefix."projects";
	$user = wp_get_current_user();
	
	// Join necessary to only return existing project ids
 	$query = "SELECT pid FROM $table, $projects WHERE pid = PROJ_ID AND uid = $user->ID";
	$rows = $wpdb->get_results($query);
	
	if (empty($rows)) return -1;
	
	foreach ($rows as $r) {
	 	$result = $r->pid;
	}
	return $result; 
} 

function current_announcement() {
	global $wpdb;
	$table = $wpdb->prefix."announcement";
	return $wpdb->get_var("SELECT `TEXT` FROM $table ORDER BY `ID` ASC");
}

function syn_load_topbar() {
		
   global $current_user;
	global $wpdb;
	global $is_iphone;
	$rightbartable = $wpdb->prefix."rightsidebar";
	$mastertable = $wpdb->prefix."master";
	
	if ($is_iphone) {
		$wpdb->query("UPDATE $mastertable SET rightbar='-1'  WHERE uid=$current_user->ID");
	}
	
	if(isset($_POST[syn_master_sidebar])) {
		$id = $_POST[syn_master_sidebar];		
		// on mobile phone open sidebar in new layer with only the sidebar and a close button
		if ($is_iphone) {
			echo '<div class="iphoneRightsidebar" id="iphoneRightsidebar">';		
			echo '<p style="text-align:right"><span onclick="syn_hide_rightsidebar();" style="cursor:pointer;">[close]</span></p>';
			switch ($id) {
				case 1:
					widget_syn_search();
					break;
				case 2:
					widget_syn_message_system();
					break;
				case 3: 
					widget_syn_user_profile();
				break;
				case 4: 
					widget_syn_rules();
					break;
				case 5:          // case 5 = help not ready yet
					break;
				default:
					break;
			}
			// echo '<p><span onclick="syn_hide_rightsidebar();" style="cursor:pointer;">[close]</span></p>';
			echo '</div>';
		} else {			
			$result = $wpdb->get_var("SELECT `uid` FROM `$mastertable` WHERE `uid`=$current_user->ID" );
			if ($result) {
				$wpdb->query("UPDATE `$mastertable` SET `rightbar`=$id  WHERE `uid`=$current_user->ID");
			} else { 
				$wpdb->insert($mastertable,  array ('rightbar'=> $id, 'uid' => $current_user->ID));
			}
		}
	}
	
	// Announcement display, special treatment for mobile phone, will disappear for 10 minutes after
	// last interaction
	echo '<div style="margin-top: 5px;border-top: dashed 1px #cccccc; padding:2px;"> </div>' ."\n";	
	if ($is_iphone) {
		echo '<img src= "'. WP_PLUGIN_URL .'/syn_master/mobile_icon2.png" style="float:left;padding-left:4px;height:30px;width:60px"/>';
		if (isset($_POST[syn_hide_announcement])) {
			update_usermeta($current_user->ID, 'no_announce', current_time('timestamp') +60); 
		}
		if (get_usermeta($current_user->ID, 'no_announce')) {
			if (current_time('timestamp') > get_usermeta($current_user->ID, 'no_announce')) {
				echo '<div id="syn_announce" style="float:left;padding-left:4px;width:70%"><p style="margin-top:0px;margin-bottom:3px">';
				echo '<b>Announcement: </b>' .  current_announcement();	
				echo '<span onclick="syn_hide_announcement();" style="cursor: pointer;"></ br> [hide]</span>';
				echo '<div style="display: none;">';
				echo '<form method="post" action="" id="hiddenAnnouncementForm"></form>';
				echo "</div>\n";
				echo "</div>\n";
			} else {
					update_usermeta($current_user->ID, 'no_announce', current_time('timestamp')+60);		
			}
		}
	} else {   // show the announcement for the normal webbrowser
		echo '<div style="float:left;padding-left:4px;width:70%"><p style="margin-top:0px;margin-bottom:3px">';
		echo '<b>Announcement: </b>' .  current_announcement();		
 		echo "</div>\n";
 	}

 	echo '<div style="float: right;text-align: right;padding-right:4px;"><p style="margin-top:0px;margin-bottom:3px">';
	echo 'You are logged in as : <b>' . $current_user->first_name . ' ' . $current_user->last_name . '</b> | ';
	if ($current_user->user_level == 10 ) 
		echo '<a href="'. get_bloginfo("url") . '/wp-admin/">Admin</a> | '. "\n";
	echo '<a href="' . wp_logout_url($redirect) . '">Logout</a> </p>'. "\n";
	
	$result = $wpdb->get_results("SELECT * FROM $rightbartable");
	$active = $wpdb->get_var("SELECT rightbar FROM $mastertable where uid=$current_user->ID");
	foreach($result as $r) {
		if ($r->id == $active) 
			echo "$r->name | ";
		else
			echo '<a  href="javascript: sidebar(' . $r->id . ')">' . $r->name . '</a> | ';
	}

	echo '</div>';
	echo	'<div style="display: none;">';
	echo	'<form method="post" action="" id="hiddenForm"></form>';
	echo "</div>\n";

	echo '<div style="clear:both;border-bottom: dashed 1px #cccccc; padding:2px;"> </div>' ."\n";
	}


function syn_master_install() {
	global $wpdb;
	$table = $wpdb->prefix."master";
	$schema = "CREATE TABLE $table (
        uid INT(9) NOT NULL,
        pid INT(9) NOT NULL,
		rightbar INT(9),
		PRIMARY KEY ( uid )
    );";
	$wpdb->query($schema);
}

add_action('activate_syn_master/syn_master.php', 'syn_master_install');
?> 
